# packages
library(tidyverse)
library(maps)
library(gridGraphics)
library(ggimage)

df <- read.csv2("americas.csv")
df_regions <- df$region
df_regions

americas <- map_data("world", region = df_regions) # get map for the americas

capitals <- world.cities %>% # get cities coord
  mutate(country.etc=case_when(
    country.etc=="Antigua and Barbuda"~"Antigua", # harmonize names with maps
    country.etc=="Saint Vincent and The Grenadines"~"Saint Vincent",
    country.etc=="Saint Kitts and Nevis"~"Saint Kitts",
    country.etc=="Trinidad and Tobago"~"Trinidad",
    TRUE ~ as.character(country.etc))) %>% 
  filter(capital==1 & # only capitals
           country.etc %in% df_regions)
capitals <- capitals[-29,] # remove duplicate San Jose

g_map <- ggplot()+
  geom_polygon(data=americas, aes(x=long, y=lat, group=group),
               color="black", fill="grey90")+
  geom_point(data=capitals, aes(x=long, y=lat))+
  coord_map(xlim=c(-140,-30),
            projection = "mollweide")+
  theme_void()

g_map

ggplot() +
  annotation_custom(ggplotGrob(g_map)) +
  annotation_custom(ggplotGrob(g_usa))
  theme_void()

ggplot()+
  coord_map(projection = "mollweide")+
  annotation_custom(grid::roundrectGrob())

df$screen <- df$region
df$screen
df[4,9] <- "Antigua\n& Barb."
df[5,9] <- "St Vincent"
df[24,9] <- "Trin. & Tob."
df[26,9] <- "Dominican\nRepublic"

for(i in 1:35){ # create doughnuts for all 35 countries
  
  namei <- df[i,1]
  df_long <- df[i,]
  df_long <- pivot_longer(df_long,
                          cols=c(2:7),
                          names_to = "variables",
                          values_to = "values")
  
  g_df <- ggplot()+
  geom_col(data=df_long,
           aes(x=variables, y=2),
           width = 1, fill="white")+
  geom_col(data=df_long,
           aes(x=variables, y=values),
           width = 1, fill="grey40", color="black", size=1)+
  geom_hline(yintercept = 1, size=1)+
  geom_hline(yintercept = 2, size=1)+
  coord_polar()+
  geom_text(data=df_long,
            aes(x=1, y=3.5, label=screen),
            hjust="inward", size=5.5, lineheight = .7)+
  theme_void()
  
  ggsave(paste0("g_",namei,".png"), g_df, device = "png",
         width = 500, height = 500, dpi = 300, units = "px")
}

g_list <- list.files(getwd(), full.names = T)
g_list <- g_list[grep("g_", g_list)]
g_list_trm <- str_sub(g_list, start=77L)
g_list_trm <- str_remove(g_list_trm, ".png")
g_list_df <- data.frame(
  country.etc=g_list_trm,
  path=g_list
)

capitals_img <- capitals
capitals_img <- capitals_img %>%
  left_join(g_list_df, by="country.etc")


si <- 0.09
map1_inset1_box <- data.frame(long=c(-64,-58,-58.5,-64.4,-64),
                              lat=c(18,18,10,10,18))
map1_inset2_box <- data.frame(long=c(-93.2,-77,-77.2,-93,-93.5),
                              lat=c(21,21,5,5,21))

map1 <- ggplot()+
  geom_polygon(data=americas, aes(x=long, y=lat, group=group),
               color="black", fill="grey90")+
  geom_image(data=capitals_img[1,], aes(x=long, y=lat, image=path), size=si)+ # paraguay
  geom_image(data=capitals_img[4,], aes(x=long, y=lat, image=path), size=si, nudge_x = 2)+ # Colombia
  geom_image(data=capitals_img[5,], aes(x=long, y=lat, image=path), size=si)+ # Brazil
  geom_image(data=capitals_img[7,], aes(x=long, y=lat, image=path), size=si, nudge_x = -5)+ # Argentina
  geom_image(data=capitals_img[8,], aes(x=long, y=lat, image=path), size=si, nudge_x = 2, nudge_y = -3)+ # Venezuela
  geom_image(data=capitals_img[10,], aes(x=long, y=lat, image=path), size=si)+ # Guyana
  geom_segment(data=capitals_img[12,], aes(x=long, xend=long-4, y=lat, yend=lat+1))+
  geom_image(data=capitals_img[12,], aes(x=long, y=lat, image=path), size=si, nudge_x = -4, nudge_y = 1)+ # Cuba
  geom_segment(data=capitals_img[13,], aes(x=long, xend=long+4, y=lat, yend=lat-4.5))+
  geom_image(data=capitals_img[13,], aes(x=long, y=lat, image=path), size=si, nudge_x = 4, nudge_y = -4.5)+ # Jamaica
  geom_image(data=capitals_img[15,], aes(x=long, y=lat, image=path), size=si)+ # Peru
  geom_image(data=capitals_img[17,], aes(x=long, y=lat, image=path), size=si, nudge_x = -2, nudge_y = 2.5)+ # Mexico
  geom_image(data=capitals_img[18,], aes(x=long, y=lat, image=path), size=si, nudge_x = 3)+ # Uruguay
  geom_segment(data=capitals_img[19,], aes(x=long, xend=long+9, y=lat, yend=lat+10))+
  geom_image(data=capitals_img[19,], aes(x=long, y=lat, image=path), size=si, nudge_x = 9, nudge_y = 10)+ # Bahamas
  geom_image(data=capitals_img[20,], aes(x=long, y=lat, image=path), size=si, nudge_x = -12, nudge_y = 10)+ # Canada
  geom_segment(data=capitals_img[22,], aes(x=long, xend=long+8, y=lat, yend=lat-1))+
  geom_image(data=capitals_img[22,], aes(x=long, y=lat, image=path), size=si, nudge_x = 8, nudge_y = -1)+ # Suriname
  geom_segment(data=capitals_img[24,], aes(x=long, xend=long+2, y=lat, yend=lat+7))+
  geom_image(data=capitals_img[24,], aes(x=long, y=lat, image=path), size=si, nudge_x = 2, nudge_y = 7)+ # Haiti
  geom_image(data=capitals_img[25,], aes(x=long, y=lat, image=path), size=si)+ # Ecuador
  geom_image(data=capitals_img[31,], aes(x=long, y=lat, image=path), size=si, nudge_x = -2)+ # Chile
  geom_segment(data=capitals_img[32,], aes(x=long, xend=long+4, y=lat, yend=lat+3))+
  geom_image(data=capitals_img[32,], aes(x=long, y=lat, image=path), size=si, nudge_x = 4, nudge_y = 3)+ # Dominican Republic
  geom_image(data=capitals_img[33,], aes(x=long, y=lat, image=path), size=si)+ # Bolivia
  geom_image(data=capitals_img[35,], aes(x=long, y=lat, image=path), size=si, nudge_x = -20)+ # USA
  
  geom_path(data=map1_inset1_box, aes(x = long, y = lat), size = 0.5) + # inset1 box
  geom_segment(data=map1_inset1_box[1,], aes(x=long, xend=long+16.8, y=lat, yend=lat+27.1))+ # inset1 upper line
  geom_segment(data=map1_inset1_box[3,], aes(x=long, xend=long+29, y=lat, yend=lat+6))+ # inset1 lower line

  geom_path(data=map1_inset2_box, aes(x = long, y = lat), size = 0.5) + # inset2 box
  geom_segment(data=map1_inset2_box[1,], aes(x=long, xend=long-30, y=lat, yend=lat-7))+ # inset1 upper line
  geom_segment(data=map1_inset2_box[3,], aes(x=long, xend=long-18.5, y=lat, yend=lat-18.2))+ # inset1 lower line
  
  coord_map(xlim=c(-140,-30),
            projection = "mollweide")+
  
  theme_void()

si_inset1 <- 5

map1_inset1_map <- ggplot()+
    geom_polygon(data=americas, aes(x=long, y=lat, group=group),
                 color="black", fill="grey90")+
  geom_segment(data=capitals_img[28,], aes(x=long, xend=long+2, y=lat, yend=lat))+
  geom_image(data=capitals_img[28,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_x = 2)+ # Antigua
  geom_image(data=capitals_img[2,], aes(x=long, y=lat, image=path), size=si*si_inset1)+ # Saint Kitts
  geom_segment(data=capitals_img[26,], aes(x=long, xend=long+3.5, y=lat, yend=lat))+
  geom_image(data=capitals_img[26,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_x = 3.5)+ # Dominica
  geom_segment(data=capitals_img[9,], aes(x=long, xend=long-2.2, y=lat, yend=lat+1))+
  geom_image(data=capitals_img[9,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_x = -2.2, nudge_y = 1)+ # Saint Lucia
  geom_image(data=capitals_img[14,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_x = 0.3, nudge_y = -0.2)+ # Saint Vincent
  geom_segment(data=capitals_img[27,], aes(x=long, xend=long-2, y=lat, yend=lat))+
  geom_image(data=capitals_img[27,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_x = -2)+ # Grenada
  geom_segment(data=capitals_img[6,], aes(x=long, xend=long+1, y=lat, yend=lat-1))+
  geom_image(data=capitals_img[6,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_x = 1, nudge_y = -1)+ # Barbados
  geom_image(data=capitals_img[23,], aes(x=long, y=lat, image=path), size=si*si_inset1)+ # Trinidad
    coord_map(xlim=c(-65,-57),
              ylim=c(19,10),
              projection = "mollweide")+
  theme_void()+
  theme(plot.background = element_rect(color="black"))

map1_inset1_map

map1_inset2_map <- ggplot()+
  geom_polygon(data=americas, aes(x=long, y=lat, group=group),
               color="black", fill="grey90")+
  geom_image(data=capitals_img[3,], aes(x=long, y=lat, image=path), size=si*si_inset1)+ # Belize
  geom_image(data=capitals_img[11,], aes(x=long, y=lat, image=path), size=si*si_inset1)+ # Guatemala
  geom_segment(data=capitals_img[30,], aes(x=long, xend=long, y=lat, yend=lat-6))+
  geom_image(data=capitals_img[30,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_y = -6)+ # El Salvador
  geom_segment(data=capitals_img[34,], aes(x=long, xend=long+4, y=lat, yend=lat+3))+
  geom_image(data=capitals_img[34,], aes(x=long, y=lat, image=path), size=si*si_inset1, nudge_x = 4, nudge_y = 3)+ # Honduras
  geom_image(data=capitals_img[16,], aes(x=long, y=lat, image=path), size=si*si_inset1)+ # Nicaragua
  geom_image(data=capitals_img[29,], aes(x=long, y=lat, image=path), size=si*si_inset1)+ # Costa Rica
  geom_image(data=capitals_img[21,], aes(x=long, y=lat, image=path), size=si*si_inset1)+ # Panama
  
  coord_map(xlim=c(-93,-77),
            ylim=c(21,5),
            projection = "mollweide")+
  theme_void()+
  theme(plot.background = element_rect(color="black"))

map1_inset2_map

df_long
df_leg <- data.frame(
  variables=c(
  "Securitization",
  "Military\nExpenditure",
  "Rare Events\nin Cybersecurity",
  "Time of the\nLegal-Institutional\nFramework",
  "Militarization\nof Cyberspace",
  "Economic\nDevelopment"
  ),
  values=c(rep(2,6))
)
df_leg$variables <- factor(df_leg$variables,
                           levels = c(
                             "Rare Events\nin Cybersecurity",
                             "Military\nExpenditure",
                             "Economic\nDevelopment",
                             "Militarization\nof Cyberspace",
                             "Securitization",
                             "Time of the\nLegal-Institutional\nFramework"
                           ))

map1_legend <- ggplot()+
  geom_col(data=df_leg,
           aes(x=variables, y=2),
           width = 1, fill="white")+
  geom_col(data=df_leg,
           aes(x=variables, y=values),
           width = 1, fill="grey40", color="black", size=1)+
  geom_hline(yintercept = 1, size=1)+
  geom_hline(yintercept = 2, size=1)+
  geom_text(data=df_leg,
            aes(x=variables, y=values+2, label=variables),
            lineheight = 1, size=2.5)+
  coord_polar(clip="off")+
  theme_void()
map1_legend

png("map1.png", width = 2000, height = 3000, res = 300)
map1
dev.off()

png("map2.png", width = 2000, height = 3000, res = 300)
map1
print(map1_inset1_map, vp = viewport(0.85, 0.65, width = 0.2, height = 0.2))
print(map1_inset2_map, vp = viewport(0.3, 0.45, width = 0.23, height = 0.23))
print(map1_legend, vp = viewport(0.25, 0.2, width = 0.32, height = 0.25))
dev.off()

